package cn.darkfire.web.Utils;

import java.util.ArrayList;
import java.util.List;

public class PageUtils<T> {
    private final static PageUtils instance = new PageUtils();

    public List<T> splitList(List<T> allList,int page,int num){
        if(page<=0){
            page = 1;
        }
        if(num<0){
            num = 10;
        }
        int pages = (allList.size()-1)/num+1;
        if(page > pages){
            page = pages;
        }
        if(allList.size() == 0){
            return allList;
        }
        List<T> list = new ArrayList<>();
        for(int i = (page-1)*num;i<page*num&&i<allList.size();i++){
            list.add(allList.get(i));
        }
        return list;

    }

    public static PageUtils getInstance() {
        return instance;
    }

    public StringBuffer getSb(List<T> allList,int page,int num){
        if(page<=0){
            page = 1;
        }
        if(num<0){
            num = 10;
        }
        int pages = (allList.size()-1)/num+1;
        if(page > pages){
            page = pages;
        }
        if(allList.size() == 0){
            return null;
        }
        StringBuffer buf = new StringBuffer();
        buf.append("<div style='color:red;display:flex;float:right'>");
        if(page>1){
            buf.append("<a href='getUserByPage.do?page="+(page-1)+"&num="+num+"'>上一页</a>");
        }
        buf.append("<p>第"+page+"页/共"+pages+"页</p>");
        buf.append("<a href='getUserByPage.do?page="+(page+1)+"&num="+num+"'>下一页</a>");
        buf.append("</div>");
        return buf;
    }

}
